﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;
using SoufunData.DB;

namespace SoufunData
{
    public partial class OfficeProjectListForm : DevExpress.XtraEditors.XtraForm
    {
        public OfficeProjectListForm()
        {
            InitializeComponent();
        }

        private bool _isDeletingProject;

        private void BProjectListForm_Load(object sender, EventArgs e)
        {
            textEdit_OpenTimeBegin.Text = "2000年1月";
            textEdit_OpenTimeEnd.Text = DateTime.Now.ToString("yyyy年M月");
            gridControl_excel.Size = new Size(1024,700);
            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }

        private void LoatData()
        {
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append(" EnterTime between '");
            strWhere.Append(DateTime.Parse(textEdit_OpenTimeBegin.Text));
            strWhere.Append("' and '");
            strWhere.Append(DateTime.Parse(textEdit_OpenTimeEnd.Text));
            strWhere.Append("' ");
            if (textEdit_proName.Text.Trim()!="")
            {
                strWhere.Append(" and   ProjectName like '%");
                strWhere.Append(textEdit_proName.Text);
                strWhere.Append("%' ");
            }


            if (textEdit_BuildBusiness.Text.Trim() != "")
            {
                strWhere.Append(" and  BuildingCompany like '%");
                strWhere.Append(textEdit_BuildBusiness.Text);
                strWhere.Append("%' ");
            }
            if (textEdit_PropertyCompany.Text.Trim() != "")
            {
                strWhere.Append(" and  PropertyCompany like '%");
                strWhere.Append(textEdit_PropertyCompany.Text);
                strWhere.Append("%' ");
            } 
            if (imgCombo_SellType.SelectedIndex != 0)
            {
                strWhere.Append(" and SaleType ='");
                strWhere.Append(imgCombo_SellType.Properties.Items[
                    imgCombo_SellType.SelectedIndex].Description);
                strWhere.Append("' ");
            }

            if (imgCombox_SaleState.SelectedIndex!=0)
            {
                strWhere.Append(" and SaleState ='");
                strWhere.Append(imgCombox_SaleState.Properties.Items[
                    imgCombox_SaleState.SelectedIndex].Description);
                strWhere.Append("' ");
            }
            if (imgeCombo_xzq.SelectedIndex!=0)
            {
                strWhere.Append(" and AdministratiVeregion ='");
                strWhere.Append(imgeCombo_xzq.Properties.Items[
                    imgeCombo_xzq.SelectedIndex].Description);
                strWhere.Append("' ");
            }

            if (imgeCombo_BusinessCircles.SelectedIndex != 0)
            {
                strWhere.Append(" and BusinessCircle ='");
                strWhere.Append(imgeCombo_BusinessCircles.Properties.Items[
                    imgeCombo_BusinessCircles.SelectedIndex].Description);
                strWhere.Append("' ");
            } 

            if (textEdit_Operator.Text.Trim()!="")
            {
                strWhere.Append(" and  Operator like '%");
                strWhere.Append(textEdit_Operator.Text);
                strWhere.Append("%' ");
            }
            if (textEdit_priceEnd.Text!=""&&
                textEdit_priceEnd.Text != "0")
            {
                strWhere.Append(" and (SalePrice Between ");
                strWhere.Append(textEdit_priceBegin.Text);
                strWhere.Append(" and ");
                strWhere.Append(textEdit_priceEnd.Text);
                strWhere.Append(" or HirePrice Between ");
                strWhere.Append(textEdit_priceBegin.Text);
                strWhere.Append(" and ");
                strWhere.Append(textEdit_priceEnd.Text);
                strWhere.Append(") ");
            } 
            if (textEdit_bulidingAreaE.Text!=""&&
                textEdit_bulidingAreaE.Text!="0")
            {
                strWhere.Append(" and BuildingArea between ");
                strWhere.Append(textEdit_bulidingAreaB.Text);
                strWhere.Append(" and ");
                strWhere.Append(textEdit_bulidingAreaE.Text);
            }
            if (textEdit_openningAreaE.Text!="" &&
                textEdit_openningAreaE.Text!="0")
            {
                strWhere.Append(" and OfficeArea between ");
                strWhere.Append(textEdit_openningAreaB.Text);
                strWhere.Append(" and ");
                strWhere.Append(textEdit_openningAreaE.Text);
            }

            try
            {
                SqlClass sqlClass = new SqlClass();
                DataSet ds = sqlClass.GetOfficeInfoList(strWhere.ToString());
                if (ds == null || ds.Tables.Count == 0)
                {
                    XtraMessageBox.Show("数据库查询出错！请检查数据库连接是否正常！");
                    return;
                }
               
                BeginInvoke(new MethodInvoker(() =>
                {
                    gridControl2.DataSource = ds.Tables[0];
                    gridControl_excel.DataSource = ds.Tables[0];
                    gridView2.GroupPanelText = "共记"
                        + ds.Tables[0].Rows.Count + "条数据";
                    btn_select.Enabled = true;
                }
                ));
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }

        

        private void btn_select_Click(object sender, EventArgs e)
        {
            if (!CheckDateEdit())
            {
                return;
            }
            BeginInvoke(new MethodInvoker(() =>
                {
                    gridView2.GroupPanelText = "正在查询...";
                    btn_select.Enabled = false;
                }));
            
            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }

   
        private bool CheckDateEdit()
        {
            if (DateTime.Parse(textEdit_OpenTimeEnd.Text)<
                DateTime.Parse(textEdit_OpenTimeBegin.Text))
            {
                XtraMessageBox.Show("搜索条件有误：开盘时间 范围中第二个值应大于第一个值！");
                return false;
            }
            if (textEdit_priceEnd.Text!="" && textEdit_priceBegin.Text!=""
                && int.Parse(textEdit_priceEnd.Text) 
                < int.Parse(textEdit_priceBegin.Text))
            {
                XtraMessageBox.Show("搜索条件有误：租金/售价 范围中第二个值应大于第一个值！");
                return false;
            }
            if (textEdit_bulidingAreaE.Text != "" 
                && textEdit_bulidingAreaB.Text != ""
                && int.Parse(textEdit_bulidingAreaE.Text)
                < int.Parse(textEdit_bulidingAreaB.Text))
            {
                XtraMessageBox.Show("搜索条件有误：建筑面积 范围中第二个值应大于第一个值！");
                return false;
            }
            if (textEdit_openningAreaE.Text != "" && 
                textEdit_openningAreaB.Text != ""
            && int.Parse(textEdit_openningAreaE.Text)
            < int.Parse(textEdit_openningAreaB.Text))
            {
                XtraMessageBox.Show("搜索条件有误：开盘面积 范围中第二个值应大于第一个值！");
                return false;
            } 
            return true;
        }

 

        private void btn_importResultExcel_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                return;
            }
            string fileName = "写字楼指标数据";
            saveFileDialog1.FileName = fileName;
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView_excel.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }

        private void textEdit_priceEnd_Leave(object sender, EventArgs e)
        {
            if (textEdit_priceEnd.Text=="0")
            {
                textEdit_priceEnd.Text = "";
            }
        }

        private void textEdit_bulidingAreaE_Leave(object sender, EventArgs e)
        {
            if (textEdit_bulidingAreaE.Text=="0")
            {
                textEdit_bulidingAreaE.Text = "";
            }
        }

        private void textEdit_openningAreaE_Leave(object sender, EventArgs e)
        {
            if (textEdit_openningAreaE.Text=="0")
            {
                textEdit_openningAreaE.Text = "";
            }
        }

        private void gridView2_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left || _isDeletingProject)
            {
                return;
            }
            DataRow row = gridView2.GetDataRow(gridView2.FocusedRowHandle);
            if (row==null)
            {
                return;
            }
             
            popupMenu_Right.ShowPopup(Cursor.Position);    
                         
        }

        private void barBtnItem_refresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btn_select_Click(null,null);
        }

        private void barBtnItem_Detail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRow row = gridView2.GetDataRow(gridView2.FocusedRowHandle);
            if (row == null)
            {
                return;
            }
            OfficeInfoDetailForm form = new OfficeInfoDetailForm(row);
            form.ShowDialog();
        }
         
    }
}